package controllers
{
	import com.pamakids.models.ResultVO;
	import com.pamakids.services.ServiceBase;
	import com.pamakids.utils.Singleton;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	import models.MatchVO;
	import models.PaintVO;
	import models.UserVO;
	import models.VO;
	import models.query.PaintQuery;
	
	import proxy.FileProxy;


	/**
	 * Service Controller
	 * @author mani
	 */
	public class API extends Singleton
	{
		private var serviceDic:Dictionary;

		private var _csrf:String;
		public function updateCsrf(csrf:String):void
		{
			_csrf = csrf;
		}
		public function getCsrf():String
		{
			return _csrf;
		}

		/**
		 *
		 */
		public function API()
		{
			serviceDic=new Dictionary();

			ServiceBase.HOST=VO.SERVICE_HOST;
		}
		
		/**
		 * 根据cid获取孩子相关数据
		 */
		public function getChildInfo(cb:Function, cid:String):void
		{
			//			var result:ResultVO = new ResultVO(true, {
			//				'cid':			cid,
			//				'portrait':		'5422649c7bcf03dd19400761/headIcons/20140924184343.png',
			//				'nickname':		'千秋',
			//				'gender':		'male',
			//				'birthday':		'2002-01-01T00:00:00.000Z',
			//			});
			//			cb(result);
			getSB('/Api/user/' + cid + '/userInfo', 'GET').call(cb);
		}
		
		public function creatMaterial(cb:Function,obj:Object):void
		{
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/material';
			getSB(req).call(cb,obj);
		}
		
		public function creatAlbumPaint(cb:Function,obj:Object):void
		{
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/album/paint';
			getSB(req).call(cb,obj);
		}
		
		public function creatQuiz(cb:Function,pid:String,obj:Object):void
		{
			var req:String = "/Api/teacher/paint/"+pid+"/question";
			getSB(req).call(cb,obj);
		}
		
		public function answerQuiz(cb:Function,qid:String,obj:Object):void
		{
			var req:String = "/Api/album/question/"+qid+"/answer";
			getSB(req).call(cb,obj);
		}
		
		public function getAlbumList(page:int,pageSize:int,cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/album';
			getSB(req,'GET').call(cb,{page:page,pageSize:pageSize});
		}
		
		public function getChannelList(age:String,cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/channel/' + age;
			getSB(req,'GET').call(cb);
		}
		
		public function creatAlbum(album:Object,cb:Function):void
		{
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/album';
			getSB(req).call(cb,album);
		}
		
		public function getDraftList(page:int,pageSize:int,cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/draft';
			getSB(req,'GET').call(cb,{page:page,pageSize:pageSize});
		}
		
		public function creatDraft(draft:Object,cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/draft';
			getSB(req).call(cb,draft);
		}
		
		public function deleteDraft(did:String,cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/draft/' +did +'/delete';
			getSB(req).call(cb);
		}
		
		public function updateDraft(draft:Object,cb:Function,did:String):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String = '/Api/teacher/'+UserVO.crtUser._id+'/draft/' + did +'/edit';
			getSB(req).call(cb,draft);
		}
		
		public function getCurPraises(pid:String,cb:Function):void
		{
			var req:String='Api/paint/'+pid+'/curatorPraise';
			getSB(req,'GET').call(cb);
		}
		
		public function getAtCount(cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/sendtimesperday';
			getSB(req,'GET').call(cb);
		}
		
		public function atFriend(pid:String,arr:Array,cb:Function,obj:Object):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint/'+pid+'/comments';
			obj['list']=arr;
			getSB(req).call(cb,obj);
		}
		
		public function atAllFans(pid:String,cb:Function,aid:String):void
		{
			var obj:Object={'following':'exist','subType':1,'data':{'data':'@了所有粉丝'}};
			if(aid)
				obj['album']=aid;
			atFriend(pid,[],cb,obj);
		}
		
		/**
		 * 1：画板贴纸 2：动画贴纸
		 * */
		public function getTexures(cb:Function,type:int):void
		{
			getSB('/Api/sticker/public','GET').call(cb,{type:type});
		}
		
		public function getTeacherTexures(cb:Function,page:int,pageSize:int):void
		{
			getSB('/Api/teacher/sticker','GET').call(cb,{page:page,pageSize:pageSize});
		}
		
		public function creatTeacherTexure(cb:Function,sid:String,title:String,cover:String,data:String):void
		{
			getSB("/Api/teacher/sticker/"+sid,'POST').call(cb,{sid:sid,title:title,cover:cover,data:data});
		}
		
		public function deleteTeacherTexture(cb:Function,sid:String,title:String,cover:String,data:String):void
		{
			var req:String = '/Api/teacher/sticker/'+sid+'/delete';
			var obj:Object = {sid:sid,title:title,cover:cover,data:data};
			getSB(req).call(cb,removeNull(obj,['sid','title','cover','data']));
		}
		
		public static function removeNull(data:Object,keys:Array):Object
		{
			var obj:Object={};
			
			for (var i:int = 0; i < keys.length; i++) 
			{
				var key:String = keys[i];
				if(data.hasOwnProperty(key))
				{
					var value:Object = data[key];
					if(value)
						obj[key]=value;
				}
			}
			
			return obj;
		}
		
		public function creatTeacherTexureGroup(cb:Function,title:String,cover:String):void
		{
			getSB("/Api/teacher/sticker",'POST').call(cb,{title:title,cover:cover});
		}
		
		public function getPrivateTextures(cb:Function,cid:String):void
		{
			var req:String = '/Api/sticker/'+cid+'/private';
			getSB(req,'GET').call(cb);
		}
		
		public function deletePrivateTexture(cb:Function,cover:String,title:String):void
		{
			var req:String = '/Api/sticker/'+UserVO.crtUser._id+'/private/delete';
			getSB(req).call(cb,{cover:cover,title:title});
		}
		
		public function createPrivateTexture(cb:Function,cover:String,title:String,data:String=null):void
		{
			var req:String = '/Api/sticker/'+UserVO.crtUser._id+'/private';
			getSB(req).call(cb,{cover:cover,title:title,data:data});
		}
		
		public function getCirclePaints(theme:String,page:int,pageSize:int,cb:Function,sort:String=''):void
		{
			var req:String='/Api/post/moment';
			var obj:Object={page:page,pageSize:pageSize,theme:theme};
			if(sort)
				obj['sort']=sort;
			getSB(req,'GET').call(cb,obj);
		}
		
		public function getCircleFriends(cid:String,theme:String,cb:Function):void
		{
			var req:String='/Api/child/'+cid+'/theme/moments';
			getSB(req,'GET').call(cb,{theme:theme});
		}
		
		public function getAlbumByPractice(practice:String,cb:Function):void
		{
			var req:String='/Api/album/practicecover';
			getSB(req,'GET').call(cb,{practicecover:practice});
		}
		
		/**
		 * 获取cid点过赞的作品数
		 * @param callback
		 * @param cid
		 */		
		public function getLikesCount(callback:Function, cid:String):void
		{
			var url:String = '/Api/child/'+cid+'/paint/likes/count';
			getSB(url, 'GET').call(callback, {cid:cid});
		}
		
		public function getBanner(cb:Function):void
		{
			getSB('/Api/paint/list/albumrecommend','GET').call(cb);
		}
		
		/**
		 * 获取cid关注的人的数量
		 * @param callback
		 * @param cid
		 */		
		public function getFollowdCount(callback:Function, cid:String):void
		{
			var url:String = '/Api/child/'+cid+'/following/count';
			getSB(url, 'GET').call(callback, {cid:cid});
		}
		
		/**
		 * 删除作品
		 */
		public function deletePaint(cid:String, pid:String, cb:Function):void
		{
			var url:String = '/Api/child/' + cid + '/paint/' + pid + '/delete';
//			var url:String = '/Api/post/' + pid + '/child/' + cid + '/delete';
			getSB(url).call(cb);
		}
		
		public function deletePost(cid:String, pid:String, cb:Function):void
		{
			//			var url:String = '/Api/child/' + cid + '/paint/' + pid + '/delete';
			var url:String = '/Api/post/' + pid + '/child/' + cid + '/delete';
			getSB(url).call(cb);
		}
		
		/**
		 * cid对ccid添加关注
		 * @param callback
		 * @param cid
		 * @param ccid
		 */		
		public function follow(callback:Function, cid:String, ccid:String):void
		{
			if(cid == ccid)
			{
				Prompt.showPromptWithBtn("不可对自己添加关注！");
				return;
			}
			
			EventTracer.instance.activePageEvent('加关注',ccid);
			
			var url:String = '/Api/child/'+cid+'/follow/'+ccid;
			getSB(url).call(callback, {cid:cid, ccid:ccid});
		}
		
		/**
		 * 获取cid的粉丝列表
		 * @param callback
		 * @param cid
		 * @param pageSize	单次请求长度
		 * @param page		页码
		 */		
		public function getFansList(callback:Function, cid:String, pageSize:uint, page:uint):void
		{
			var url:String = '/Api/child/' + cid + '/followed';
			var obj:Object = {pageSize: pageSize, page:page, cid:cid};
			getSB(url, 'GET').call(callback, obj);
		}
		
		/**
		 * 获取cid的关注列表
		 * @param callback
		 * @param cid
		 * @param pageSize	单次请求长度
		 * @param page		页码
		 */		
		public function getFollowedList(callback:Function, cid:String, pageSize:uint, page:uint):void
		{
			var url:String = '/Api/child/' + cid + '/following';
			var obj:Object = {pageSize: pageSize, page:page, cid:cid};
			getSB(url, 'GET').call(callback, obj);
		}
		
		/**
		 * 获取指定cid已经点赞的所有作品
		 * @param callback
		 * @param cid
		 */		
		public function getChildLikes(callback:Function, cid:String, page:uint, pageSize:uint):void
		{
			var url:String = '/Api/child/'+cid+'/paint/any/likes';
//			var url:String = '/Api/post/list/following';
			getSB(url, 'GET').call(callback, {cid:cid, page:page, pageSize:pageSize});
		}
		
		/**
		 * 查看cid是否对ccid添加了关注
		 * @param callback
		 * @param cid
		 * @param ccid
		 */		
		public function isFollowed(callback:Function, cid:String, ccid:String):void
		{
			var url:String = '/Api/child/' + cid + '/followed/' + ccid;
			getSB(url, 'GET').call(callback, {cid:cid, ccid:ccid});
		}
		
		public function getPaintsByAuthorID(callback:Function, cid:String):void
		{
			var url:String = '/Api/paint/list/all';
			getSB(url, 'GET').call(callback, {author: cid});
		}
		
		/**
		 * 获取cid的粉丝数
		 * @param callback
		 * @param cid
		 */		
		public function getFansCount(callback:Function, cid:String):void
		{
			var url:String = '/Api/child/'+cid+'/followed/count';
			getSB(url, 'GET').call(callback, {cid:cid});
		}
		
		public function addParentShare(pid:String):void
		{
			var url:String = '/Api/paint/'+pid+'/add/shareToParent';
			getSB(url).call(function(o:Object):void{
				trace(o);
			});
		}

		/**
		 * @return
		 */
		public static function get instance():API
		{
			return Singleton.getInstance(API);
		}

		public function paintGet(callback:Function, query:Object):void
		{
			getSB('/Api/paint/list/all', 'GET').call(callback, query);
		}
		
		public function classroomList(callback:Function, page:int,pageSize:int,type:int):void
		{
			getSB('/Api/paint/list/college', 'GET').call(callback, {page:page,pageSize:pageSize,collegeType:type});
		}

		public function getPaintDetail(callback:Function, pid:String):void
		{
			getSB('/Api/teacher/paint/'+pid, 'GET').call(callback);
			
//			getSB('/Api/paint/'+pid, 'GET').call(callback);
		}

		public function matchPaintGet(callback:Function, mid:String,query:PaintQuery):void
		{
			var req:String='/Api/match/'+mid+'/paint';
			getSB(req, 'GET').call(callback, query);
		}

		public function getMatchMedals(callback:Function, mid:String):void
		{
			var req:String='/Api/match/'+mid+'/info';
			getSB(req, 'GET').call(callback);
		}

		public function userPaintGet(callback:Function,userID:String):void
		{
			var req:String='/Api/child/'+userID+'/paint';
			getSB(req, 'GET').call(callback);
		}

		public function themePaintGet(callback:Function,theme:String, date:String,page:int,pageSize:int=10):void
		{
			getSB('/Api/paint/list/date', 'GET').call(callback, {'theme':theme,'pageSize':pageSize,'page':page,'date':date});
		}
		
		public function comments(cb:Function, pid:String, subType:int, data:String, length:int,target:String):void
		{
			var cid:String = UserVO.crtUser._id;
			var url:String = '/Api/child/' + cid + '/paint/' + pid + '/comments';
			getSB(url).call(cb, {'subType':subType, 'data':data, 'length':length,'target':target});
			EventTracer.instance.activePageEvent(target?'对人评论':'对画评论',target,pid,{'subType':subType,'data':data});
		}
		
		public function getAlbumDetail(callback:Function,aid:String):void
		{
			var req:String='/Api/album/'+aid;
			getSB(req, 'GET').call(callback);
		}
		
		public function getArtRoom(callback:Function):void
		{
			var req:String='/Api/paint/list/artRoom';
			getSB(req, 'GET').call(callback);
		}
		
		public function getStoryRoom(callback:Function):void
		{
			var req:String='/Api/paint/list/storyRoom';
			getSB(req, 'GET').call(callback);
		}

		public function webLogin(uid:String,pw:String,nickname:String,portrait:String,school:String,clazz:String,callback:Function,
								 province_name:String,city_name:String,county_name:String,gradeLevel:String):void
		{
			getSB('/csrf/'+VO.FIRST_CSRF, 'GET').call(
				function(o:ResultVO):void
				{
					getSB('/Api/user/login/web').call(callback, {'username': uid, 'password': pw,'nickname':nickname,
							'portrait':portrait,'school':school,'grade':clazz,'gradeLevel':gradeLevel,
							'province_name':province_name,'city_name':city_name,'county_name':county_name,
							'come_from':'17zuoye'});
				}
				);
		}

		/**
		 *@param paintID
		 *@param page
		 * results : [PraiseVO]
		 * */
		public function getPraiseList(callback:Function, paintID:String,page:int,pageSize:int):void
		{
//			var req:String='/Api/paint/'+paintID+'/voteAndLikes';
			var req:String='/Api/paint/'+paintID+'/praises';
			getSB(req, 'GET').call(callback,{'page':page,'pageSize':pageSize});
//			getSB('/paint/praise/list', 'GET').call(callback, {'paint': paintID, 'page': page});
		}

		public function getPaintPraised(cb:Function, pid:String):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint/'+pid+'/likes';
			getSB(req, 'GET').call(cb);
		}

		/**
		 *
		 * @param callback
		 * @param paint
		 */
		public function paintAdd(callback:Function, paint:Object):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint';
//			paint['public']=0;
			getSB(req).call(callback, paint);
		}
		
		public function addPrivateTexture(callback:Function, texture:Object):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/texture';
			getSB(req).call(callback, texture);
		}
		
		public function animeAdd(callback:Function, paint:Object):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='Api/child/'+UserVO.crtUser._id+'/animations';
			getSB(req).call(callback, paint);
		}
		
		public function sharePaint(callback:Function,pid:String,audio:String,name:String):void
		{
			EventTracer.instance.activePageEvent('分享','',pid);
			
			var paint:Object={};
			paint['public']=1;
			if(audio)
				paint['audio']=audio;
			if(name)
				paint['name']=name;
			updatePaint(pid,paint,callback);
		}

		public function matchPaintAdd(callback:Function, paint:Object):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/match/'+MatchVO.crtMatch._id+'/paint';
			paint['public']=2;
			getSB(req).call(callback, paint);
		}

		public function paintDelete(callback:Function, paint:PaintVO):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint/'+
				paint._id+'/delete'
			getSB(req).call(callback);
		}

		/**
		 *
		 * @param user
		 * @param paintCB results:[PaintVO]
		 */
//		public function getPaintList(user:UserVO, paintCB:Function, page:int):void
//		{
//			var pq:PaintQuery=new PaintQuery();
//			pq.page=page;
//			getSB('/paint/list', 'GET').call(paintCB, pq);
//		}

		/**
		 * @param id
		 * results:PaintVO
		 */
//		public function getPaintDetail(cb:Function, id:String):void
//		{
//			getSB('/Api/paint/'+id, 'GET').call(cb);
//		}

		public function addPaintPlayedCount(cb:Function, id:String):void
		{
			var req:String='/Api/paint/'+id+'/add/play';
			getSB(req).call(cb);
		}

		public function addPaintWatchedCount(cb:Function, id:String):void
		{
			var req:String='/Api/paint/'+id+'/add/view';
			getSB(req).call(cb);
		}

		public function praisePaint(paint:Object, cb:Function,islike:Boolean,useType:Boolean=false):void
		{
			if(!UserVO.crtUser)
				return;
			EventTracer.instance.activePageEvent(islike?'点赞':'取消赞',paint.author?paint.author._id:'',paint._id);
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint/'+((useType&&paint.typeId)?paint.typeId:paint._id)+(islike?'/likes':'/dislike');
			getSB(req).call(cb);
//			getSB('/paint/praise', "GET").call(cb, {"paint": paint._id});
		}

		public function getPaintReported(cb:Function,pid:String,ccid:String):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/report/'+ccid+'/paint/'+pid;
			getSB(req,'GET').call(cb);
		}

		public function reportPaint(cb:Function,pid:String,ccid:String):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/report/'+ccid+'/paint/'+pid;
			getSB(req).call(cb);
		}
		
		/**
		 * 获取画作数据
		 * @param callback
		 * @param pageSize	一次请求的长度
		 * @param page		页码
		 * @param sort		排序： 默认以时间排序, 传参“agrees”则按照人气排序
		 */		
		public function getPaintList(callback:Function, page:uint, pageSize:uint=30, sort:String=null, authorID:String=null, pubValue:Object=1):void
		{
			var url:String = '/Api/paint/list/all';
			var data:Object = {pageSize: pageSize, page:page};
			if(sort)
				data.sort = sort;
			if(authorID)
				data.author = authorID;
			data['public'] = pubValue;
			
			if(UserVO.crtUser)
				data.childId = UserVO.crtUser._id;
			getSB(url, 'GET').call(callback, data);
		}
		
		public function getPaintList2(callback:Function, page:uint, pageSize:uint=30, sort:String=null, authorID:String=null):void
		{
			var url:String = '/Api/post/list/all';
			var data:Object = {pageSize: pageSize, page:page};
			if(sort)
				data.sort = sort;
			if(authorID)
				data.author = authorID;
			
			if(UserVO.crtUser)
				data.childId = UserVO.crtUser._id;
			getSB(url, 'GET').call(callback, data);
		}

		/**
		 *
		 * @param callback
		 */
		public function getUploadToken(callback:Function):void
		{
			getSB('/upload/token', 'GET').call(callback);
		}

		/**
		 *
		 * @param callback
		 */
		public function getZelaUploadToken(callback:Function):void
		{
//			getSB('/Api/upload/token/'+ServiceBase.id, 'GET').call(callback);
		}

		/**
		 *
		 * @param update
		 * @param callback
		 */
//		public function userUpdate(update:Object, callback:Function):void
//		{
//			getSB('/user/update').call(callback, update);
//		}

		/**
		 *
		 * @param update  {"nickname":nickname,"portrait":portrait,"email":,email}
		 * @param callback
		 */
//		public function userUpdateUser(update:Object, callback:Function):void
//		{
//			getSB('/user/update').call(callback, update);
//		}

		/**
		 *
		 * @param usernameOrEmailOrUSid
		 * @param callback
		 */
//		public function userExist(usernameOrEmailOrUSid:Object, callback:Function):void
//		{
//			getSB('/user/exist', 'GET').call(callback, usernameOrEmailOrUSid);
//		}

		private function getSB(uri:String, method:String='POST'):ServiceBase
		{
			var s:ServiceBase=serviceDic[uri + method];
			if (s)
				return s;
			s=new ServiceBase(uri, method);
			serviceDic[uri + method]=s;
			return s;
		}

		/**
		 * @param oldPW
		 * @param newPW
		 * @param cb status:boolean
		 */
//		public function changePW(oldPW:String, newPW:String, cb:Function):void
//		{
//			getSB('/user/update/password').call(cb, {"old": oldPW, "new": newPW});
//		}

		/**
		 *
		 * @param user
		 * @param cb results: [RelationshipVO]
		 */
		public function getFollowList(cb:Function,userID:String,page:int,pageSize:int):void
		{
			var req:String='/Api/child/'+userID+'/following';
			getSB(req, "GET").call(cb, {'pageSize':pageSize,'page':page});
		}

		/**
		 *
		 * @param user
		 * @param cb results: [RelationshipVO]
		 */
		public function getFanList(cb:Function,userID:String,page:int,pageSize:int):void
		{
			var req:String='/Api/child/'+userID+'/followed';
			getSB(req, "GET").call(cb, {'pageSize':pageSize,'page':page});
		}

		public function getFollowed(cb:Function,guestID:String):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/followed/'+guestID;
			getSB(req, "GET").call(cb);
		}

		/**
		 *
		 * @param friendID
		 * @param followed
		 * @param cb staus:boolean
		 */
		public function followFriend(cb:Function,friendID:String,isFollow:Boolean):void
		{
			if(!UserVO.crtUser)
				return;
			
			EventTracer.instance.activePageEvent(isFollow?'加关注':'取消关注',friendID);
			
			var req:String='/Api/child/'+UserVO.crtUser._id+
				(isFollow?'/follow/':'/unfollow/')+friendID
			getSB(req).call(cb);
//			getSB('/user/relationship').call(cb, {"friend": friendID, "follow": followed});
		}

		public function initToken():void
		{
//			getSB('/upload/token', "GET").call(saveToken);
//			getSB('/upload/audio/token', "GET").call(saveAudioToken);
			getSB('/Api/upload/token/'+ServiceBase.id, "GET").call(saveToken);
			getSB('/Api/upload/audio/token/'+ServiceBase.id, "GET").call(saveAudioToken);
			getSB('/Api/upload/video/token/'+ServiceBase.id, "GET").call(saveVideoToken);
		}

		private function saveToken(o:ResultVO):void
		{
			if (o.status)
				FileProxy.token=o.results as String;
		}

		private function saveAudioToken(o:ResultVO):void
		{
			if (o.status)
				FileProxy.audioToken=o.results as String;
		}
		
		private function saveVideoToken(o:ResultVO):void
		{
			if (o.status)
				FileProxy.videoToken=o.results as String;
		}

		public function getMsgList(cb:Function,page:int,pageSize:int):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/messages/';
			getSB(req, "GET").call(cb,{'page':page,'pageSize':pageSize});
		}

		//===============================version_2====================================

		/**
		 *
		 * @param obj	{mobile:""/email:"", password:""}
		 * @param callback
		 */
		public function login(obj:Object, callback:Function):void
		{
			getSB('/Api/user/login').call(callback, obj);
		}
		
		public function initCSRF():void
		{
			getSB('/csrf/'+VO.FIRST_CSRF, 'GET').call(null);
		}
		
		public function loginTeacher(mobile:String,password:String, callback:Function):void
		{
			getSB('/csrf/'+VO.FIRST_CSRF, 'GET').call(
				function(o:ResultVO):void
				{
					getSB('/Api/user/login/curator').call(callback, {mobile:mobile,password:password,come_from:VO.COME_FROM});
				}
			);
		}
		
		public function msgRead(cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/messages/read';
			getSB(req).call(cb);
		}

		/**
		 * 从第三方平台登录，其中iconURL对应portrait，userName对应nickname，accessToken对应access_token，platformName对应platform
		 * {
		 accessToken = "2.00twwPQC03t9ZI6ef9cf7509eaFhTB";
		 iconURL = "http://tp4.sinaimg.cn/2072488563/180/40012495145/1";
		 platformName = sina;
		 profileURL = "http://www.weibo.com/u/2072488563";
		 userName = "\U6298\U7ffc\U4f34\U4f60\U884c";
		 usid = 2072488563;
		 }
		 * @param info
		 * @param callback
		 */
		public function loginFromPlatform(info:Object, callback:Function):void
		{
			getSB('/Api/user/login/platform').call(callback, info);
		}

		/**
		 * 注册用户
		 * @param user 用户对象
		 * @param callback 注册回调
		 */
		public function register(user:Object, callback:Function):void
		{
			getSB('/Api/user/register').call(callback, user);
		}
		
		/**
		 * 注册老师
		 * @param mobile 手机
		 * @param password 密码
		 * @param callback 注册回调
		 */
		public function registerTeacher(mobile:String,password:String, callback:Function):void
		{
			getSB('/Api/user/register').call(callback, {mobile:mobile,password:password,come_from:VO.COME_FROM,curator:1});
		}
		
		/**
		 * 修改孩子资料，包括nickname, portrait, address, birthday
		 * @param cb
		 * @param obj {uid, nickname, portrait, address, birthday}
		 */		
		public function changeTeacherInfo(cb:Function, obj:Object):void
		{
			var url:String = '/Api/user/update/' + obj.uid;
			getSB(url).call(cb, obj);
		}

		/**
		 * 向服务端发送验证码
		 * @param data	{mobile:"", email:"", register_token:""}必须有一个
		 * @param callback
		 *
		 */		
		public function sendVerify(data:Object, callback:Function):void
		{
			getSB('/Api/user/register/verify').call(callback, data);
		}

		/**
		 * 重新获取验证码(注册时)
		 * @param data
		 * @param callback
		 */		
		public function resendVerify(data:Object, callback:Function):void
		{
			getSB('/Api/user/register/resend').call(callback, data);
		}

		/**
		 * 获取验证码（重置密码时）
		 * @param data
		 * @param callback
		 */		
		public function resetPwdVerify(data:Object, callback:Function):void
		{
			getSB('/Api/user/reset').call(callback, data);
		}

		/**
		 * 强制重置密码
		 * @param data
		 * @param callback
		 */		
		public function resetPwd(data:Object, callback:Function):void
		{
			getSB('/Api/user/update/resetPassword').call(callback, data);
		}

		/**
		 * 发送密码重置的验证码
		 * @param data
		 * @param callback
		 */		
		public function sendPwdVerify(data:Object, callback:Function):void
		{
			getSB('/Api/user/reset/verify').call(callback, data);
		}

//		/**
//		 * 获取孩子数据
//		 * @param data
//		 * @param callback
//		 */		
//		public function getChildrenInfo(data:Object, callback:Function):void
//		{
//			if(!UserVO.crtUser)
//				return;
//			var url:String = "/Api/user/" + UserVO.crtUser._id + "/child";
//			getSB(url, 'GET').call(callback);
//		}


		/**
		 * 创建孩子账号
		 * @param data
		 * @param callback
		 */		
		public function creatChild(data:Object, callback:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var url:String = "/Api/user/" + UserVO.crtUser._id + "/child";
			getSB(url).call(callback, data);
		}

//		public function creatPaint(cover:String,data:String,callback:Function):void
//		{
//			var url:String = '/Api/child/'+UserVO.crtUser.child+'/paint';
//			getSB(url).call(callback,{'cover':cover,'data':data});
//		}

		public function updatePaint(paintID:String,data:Object,callback:Function):void
		{
			var url:String = '/Api/paint/'+paintID;
			getSB(url).call(callback,data);
		}

		public function getChildPaints(callback:Function,cid:String):void
		{
			var url:String = '/Api/child/'+cid+'/paint';
			getSB(url,'GET').call(callback,{'public':'$gt:0'});
		}

		/**
		 * 设备号登录
		 * @param data
		 * @param callback
		 */		
		public function loginByDeviceNum(data:Object, callback:Function):void
		{
			getSB('/Api/user/login/device').call(callback, data);
		}

		public function getCurrentMatch(callback:Function):void
		{
			getSB('/Api/match/current','GET').call(callback);
		}

		public function getMatchDetail(callback:Function,mid:String):void
		{
			getSB('/Api/match/'+mid,'GET').call(callback);
		}
		
		/**
		 * 读取时告知后台，以取消消息的未读状态
		 * @param cb
		 */		
		public function readMsg(cb:Function):void
		{
			var url:String = '/Api/child/' + UserVO.crtUser._id + '/messages/read';
			//			var url:String = '/Api/child/53c8c844df667afa257a5224/messages/read';
			getSB(url).call(cb, {cid: UserVO.crtUser._id});
		}

		public function getAllMatches(matchSize:int,matchPage:int,paintPage:int,paintSize:int,cb:Function=null):void
		{
			getSB('/Api/match/list/all','GET').call(function(o:ResultVO):void{
				if(o.status)
				{
					if(o.results)
					{
						MatchVO.matchList=new ArrayCollection(o.results as Array);
						if(cb!=null)
							cb();
						cb=null;
					}else
					{

					}
				}
			},{'matchSize':matchSize,'matchPage':matchPage,'paintPage':paintPage,'paintSize':paintSize});
		}

		public function vote(cb:Function,pid:String):void
		{
			if(!UserVO.crtUser)
				return;
			
			EventTracer.instance.activePageEvent('投票','',pid);
			
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint/'+pid+'/vote';
			getSB(req).call(cb);
		}

		public function getMoreDetail(cb:Function,cid:String):void
		{
			var req:String='/Api/user/'+cid+'/userMoreInfo';
			getSB(req,'GET').call(cb);
		}
		
		public function getUserTask(cb:Function,cid:String):void
		{
			var req:String='/Api/user/'+cid+'/userTask';
			getSB(req,'GET').call(cb);
		}
		
		public function addShareReward(cb:Function,pid:String):void
		{
			var req:String='/Api/paint/'+pid+'/add/share';
			getSB(req).call(cb);
		}
		
		public function signUp(cb:Function,cid:String):void
		{
			var req:String='/Api/child/'+cid+'/signUp';
			getSB(req).call(cb);
		}

		public function getRecommend(cb:Function):void
		{
			getSB('Api/paint/list/recommend','GET').call(cb);
		}

		public function addComment(cb:Function,pid:String,type:String):void
		{
			if(!UserVO.crtUser)
				return;
			
			EventTracer.instance.activePageEvent('固定评论'+type,'',pid);
			
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint/'+pid+'/enumlikes';
			getSB(req,'POST').call(cb,{subType:type});
		}
		
		public function getComments(cb:Function,pid:String):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/paint/any/enumlikes';
			getSB(req,'POST').call(cb,{'any':pid});
		}
		
		public function getPaintCommentList(cb:Function, pid:String, page:uint=1, pageSize:uint=10):void
		{
			var url:String = '/Api/paint/' + pid + '/comments';
			getSB(url, 'GET').call(cb, {'page':page, 'pageSize':pageSize});
		}
		
		public function getDaysRank(cb:Function,pagesize:int,page:int,day:int):void
		{
			var req:String='/Api/paint/list/rank';
			getSB(req,'GET').call(cb,{'pageSize':pagesize,'page':page,'day':day});
		}
		
		public function exit(cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/user/'+UserVO.crtUser._id+'/exit';
			getSB(req,'GET').call(cb);
		}
		
		public function getRecommendList(cb:Function,uid:String,page:int,pageSize:int):void
		{
//			var url:String = 'Api/paint/list/following';
			var url:String = 'Api/post/list/following';
			getSB(url, 'GET').call(cb, {'uid':uid,'page':page, 'pageSize':pageSize});
		}
		
		//		public function checkLiked(pid:String, callback:Function):void
		//		{
		//			var url:String = "/Api/child/" + UserVO.crtUser._id + "/paint/" + pid + "/likes";
		//			getSB(url, 'GET').call(callback);
		//		}
		
		/**
		 * 获取对pid点过赞的用户列表
		 * @param cb
		 * @param pid
		 * @param page
		 * @param pageSize
		 *
		 */		
		public function getPaintPraiseList(cb:Function, pid:String, page:uint=1, pageSize:uint=10):void
		{
			var url:String = '/Api/paint/' + pid + '/likes';
			getSB(url, 'GET').call(cb, {'page':page, 'pageSize':pageSize});
		}
		
		public function getMasterList(cb:Function):void
		{
			var url:String = '/Api/user/list/diamond';
			getSB(url, 'GET').call(cb);
		}
		
		public function getPingList(cb:Function):void
		{
			var url:String = '/Api/user/list/diamond';
			getSB(url, 'GET').call(cb);
		}
		
		public function getShareReward(pid:String,cb:Function):void
		{
			var url:String = '/Api/paint/'+pid+'/add/share';
			getSB(url, 'GET').call(cb);
		}
		
		public function getToken(cb:Function=null, key:String=null):void
		{
			var obj:Object;
			if(key)
				obj = {'key': key};
			getSB('/Api/upload/token/' + ServiceBase.id, 'GET').call(cb, obj);
		}
		
		public function getAudioToken(cb:Function=null, key:String=null):void
		{
			var obj:Object;
			if(key)
				obj = {'key': key};
			getSB('/Api/upload/audio/token/'+ServiceBase.id, 'GET').call(cb, obj);
		}
		
		public function getVideoToken(cb:Function=null, key:String=null):void
		{
			var obj:Object;
			if(key)
				obj = {'key': key};
			getSB('/Api/upload/video/token/'+ServiceBase.id, 'GET').call(cb, obj);
		}

		public function getGuide(cb:Function):void
		{
			if(!UserVO.crtUser)
				return;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/guide';
			getSB(req,'GET').call(cb);
		}
		
		public function platformLogin(cb:Function, usid:String,access_token:String,  nickname:String, portrait:String, come_from:String='webqq', platform:String='qq'):void
		{
			getSB('/csrf/'+VO.FIRST_CSRF, 'GET').call(
				function(o:ResultVO):void
				{
					getSB('Api/user/login/platform','POST').call(cb,{'access_token':access_token,'usid':usid, portrait:portrait,nickname:nickname,come_from:come_from,platform:platform});
				}
			);
		}

		/**
		 * 1:首页修改-更新通知
		 * 2:录音界面关闭后都提示
		 * 3:未公开的作品
		 * 
		 * */
		public function guideViewed(cb:Function,step:String):void
		{
			if(!UserVO.crtUser)
				return;
			VO['step'+step]++;
			var req:String='/Api/child/'+UserVO.crtUser._id+'/guide';
			getSB(req,'POST').call(cb,{('step'+step):VO['step'+step]});
		}
		
	}
}


